﻿using System;
using System.Windows.Forms;
using GDFlacTool.Properties;

namespace GDFlacTool.Forms
{
    public partial class FormConverterMp3 : Form
    {
        private readonly bool _isWaveMp3;

        private string _frequency = string.Empty;
        private string _channels = string.Empty;
        private string _copyright = string.Empty;
        private string _original = string.Empty;
        private string _crcErrorProtection = string.Empty;
        private string _forceIsoCompability = string.Empty;

        public FormConverterMp3(bool isWaveMp3)
        {
            InitializeComponent();

            _isWaveMp3 = isWaveMp3;

            LoadFrequencyCombo();
            LoadChannelsCombo();
        }

        public string Frequency
        {
            get { return _frequency; }
        }

        public string Channels
        {
            get { return _channels; }
        }

        public string Copyright
        {
            get { return _copyright; }
        }

        public string Original
        {
            get { return _original; }
        }

        public string CrcErrorProtection
        {
            get { return _crcErrorProtection; }
        }

        public string ForceIsoCompability
        {
            get { return _forceIsoCompability; }
        }

        private void LoadFrequencyCombo()
        {
            cboFrequency.Add(imlAudio.Images["Frequency"], "[as source]");
            cboFrequency.Add(imlAudio.Images["Frequency"], "8 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "11 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "12 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "16 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "22 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "24 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "32 KHz");
            cboFrequency.Add(imlAudio.Images["Frequency"], "44,1 KHz (CD)");
            cboFrequency.Add(imlAudio.Images["Frequency"], "48 KHz (DAT)");
        }

        private void LoadChannelsCombo()
        {
            cboChannels.Add(imlAudio.Images["Stereo"], "[auto]");
            cboChannels.Add(imlAudio.Images["Mono"], "'mono'");
            cboChannels.Add(imlAudio.Images["Stereo"], "'stereo [auto]' (CD)");
            cboChannels.Add(imlAudio.Images["Stereo"], "'joint stereo' (CD)");
            cboChannels.Add(imlAudio.Images["Stereo"], "'forced joint stereo' (CD)");
            cboChannels.Add(imlAudio.Images["Stereo"], "'dual channels' (CD)");
            cboChannels.Add(imlAudio.Images["Stereo"], "'forced stereo' (CD)");
        }

        private void cboFrequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboFrequency.Text)
            {
                case "[as source]":
                    _frequency = string.Empty;
                    break;
                case "8 KHz":
                    _frequency = string.Format("-freq=\"8000\"");
                    break;
                case "11 KHz":
                    _frequency = string.Format("-freq=\"11000\"");
                    break;
                case "12 KHz":
                    _frequency = string.Format("-freq=\"12000\"");
                    break;
                case "16 KHz":
                    _frequency = string.Format("-freq=\"16000\"");
                    break;
                case "22 KHz":
                    _frequency = string.Format("-freq=\"22000\"");
                    break;
                case "24 KHz":
                    _frequency = string.Format("-freq=\"24000\"");
                    break;
                case "32 KHz":
                    _frequency = string.Format("-freq=\"32000\"");
                    break;
                case "44,1 KHz (CD)":
                    _frequency = string.Format("-freq=\"41000\"");
                    break;
                case "48 KHz (DAT)":
                    _frequency = string.Format("-freq=\"48000\"");
                    break;
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _frequency = _frequency.Replace("\"", "{qt}");
            }
        }

        private void cboChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboChannels.Text)
            {
                case "[auto]":
                    _channels = string.Empty;
                    break;
                case "'mono'":
                    _channels = string.Format("-channels=\"mono\"");
                    break;
                case "'stereo [auto]' (CD)":
                    _channels = string.Format("-channels=\"stereo\"");
                    break;
                case "'joint stereo' (CD)":
                    _channels = string.Format("-channels=\"joint stereo\"");
                    break;
                case "'forced joint stereo' (CD)":
                    _channels = string.Format("-channels=\"forced joint stereo\"");
                    break;
                case "'dual channels' (CD)":
                    _channels = string.Format("-channels=\"dual channels\"");
                    break;
                case "'forced stereo' (CD)":
                    _channels = string.Format("-channels=\"forced stereo\"");
                    break;
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _channels = _channels.Replace("\"", "{qt}");
            }
        }

        private void FormConverterMp3_Load(object sender, EventArgs e)
        {
            if (_isWaveMp3)
            {
                cboFrequency.SelectedIndex = AudioConverterSettings.Default.cboWaveMp3Frequency_SelectedIndex;
                cboChannels.SelectedIndex = AudioConverterSettings.Default.cboWaveMp3Channels_SelectedIndex;
                chkCopyright.Checked = AudioConverterSettings.Default.chkWaveMp3Copyright_Checked;
                chkOriginal.Checked = AudioConverterSettings.Default.chkWaveMp3Original_Checked;
                chkCrcErrorProtection.Checked = AudioConverterSettings.Default.chkWaveMp3CrcErrorProtection_Checked;
                chkForceIsoCompability.Checked = AudioConverterSettings.Default.chkWaveMp3ForceIsoCompability_Checked;
            }
            else
            {
                cboFrequency.SelectedIndex = AudioConverterSettings.Default.cboMp3Frequency_SelectedIndex;
                cboChannels.SelectedIndex = AudioConverterSettings.Default.cboMp3Channels_SelectedIndex;
                chkCopyright.Checked = AudioConverterSettings.Default.chkMp3Copyright_Checked;
                chkOriginal.Checked = AudioConverterSettings.Default.chkMp3Original_Checked;
                chkCrcErrorProtection.Checked = AudioConverterSettings.Default.chkMp3CrcErrorProtection_Checked;
                chkForceIsoCompability.Checked = AudioConverterSettings.Default.chkMp3ForceIsoCompability_Checked;
            }
        }

        private void FormConverterMp3_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_isWaveMp3)
            {
                AudioConverterSettings.Default.cboWaveMp3Frequency_SelectedIndex = cboFrequency.SelectedIndex;
                AudioConverterSettings.Default.cboWaveMp3Channels_SelectedIndex = cboChannels.SelectedIndex;
                AudioConverterSettings.Default.chkWaveMp3Copyright_Checked = chkCopyright.Checked;
                AudioConverterSettings.Default.chkWaveMp3Original_Checked = chkOriginal.Checked;
                AudioConverterSettings.Default.chkWaveMp3CrcErrorProtection_Checked = chkCrcErrorProtection.Checked;
                AudioConverterSettings.Default.chkWaveMp3ForceIsoCompability_Checked = chkForceIsoCompability.Checked;
            }
            else
            {
                AudioConverterSettings.Default.cboMp3Frequency_SelectedIndex = cboFrequency.SelectedIndex;
                AudioConverterSettings.Default.cboMp3Channels_SelectedIndex = cboChannels.SelectedIndex;
                AudioConverterSettings.Default.chkMp3Copyright_Checked = chkCopyright.Checked;
                AudioConverterSettings.Default.chkMp3Original_Checked = chkOriginal.Checked;
                AudioConverterSettings.Default.chkMp3CrcErrorProtection_Checked = chkCrcErrorProtection.Checked;
                AudioConverterSettings.Default.chkMp3ForceIsoCompability_Checked = chkForceIsoCompability.Checked;
            }

            AudioConverterSettings.Default.Save();
        }

        private void chkCopyright_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCopyright.Checked)
            {
                _copyright = string.Format("-copyright_bit=\"1\"");
            }
            else
            {
                _copyright = string.Empty;
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _copyright = _copyright.Replace("\"", "{qt}");
            }
        }

        private void chkOriginal_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOriginal.Checked)
            {
                _original = string.Empty;
            }
            else
            {
                _original = string.Format("-original_bit=\"0\"");
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _original = _original.Replace("\"", "{qt}");
            }
        }

        private void chkCrcErrorProtection_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCrcErrorProtection.Checked)
            {
                _crcErrorProtection = string.Format("-crc=\"1\"");
            }
            else
            {
                _crcErrorProtection = string.Empty;
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _crcErrorProtection = _crcErrorProtection.Replace("\"", "{qt}");
            }
        }

        private void chkForceIsoCompability_CheckedChanged(object sender, EventArgs e)
        {
            if (chkForceIsoCompability.Checked)
            {
                _forceIsoCompability = string.Format("-force_iso=\"1\"");
            }
            else
            {
                _forceIsoCompability = string.Empty;
            }

            if (_isWaveMp3 && !string.IsNullOrEmpty(_frequency))
            {
                _forceIsoCompability = _forceIsoCompability.Replace("\"", "{qt}");
            }
        }
    }
}
